﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Windows.Forms;

namespace SnmpForm.APConfig
{
    public partial class frmWlanClientList : frmApConfig
    {
        public frmWlanClientList(BusinessWrapper.Ap ap):base(ap)
        {
            InitializeComponent();
            listViewClients.Columns.Add("编号", 40);
            listViewClients.Columns.Add("MAC", 120);
            listViewClients.Columns.Add("AID");
            listViewClients.Columns.Add("CHAN");
            listViewClients.Columns.Add("RATE");
            listViewClients.Columns.Add("RSSI");
            listViewClients.Columns.Add("IDLE");
            listViewClients.Columns.Add("TXSEQ");
            listViewClients.Columns.Add("RXSEQ");
            listViewClients.Columns.Add("CAPS");
            listViewClients.Columns.Add("ACAPS");
            listViewClients.Columns.Add("ERP");
            listViewClients.Columns.Add("HTCAPS");
            string strClientList = "";
            try
            {
                strClientList = m_ap.ScanWirelessList();
            }
            catch (Lextm.SharpSnmpLib.Messaging.TimeoutException ex)
            {
                Console.WriteLine(ex.Message.ToString());
                MessageBox.Show("同步超时");
            }
            catch (SocketException ex)
            {
                Console.WriteLine(ex.Message.ToString());
                MessageBox.Show("连接异常");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message.ToString());
                MessageBox.Show(ex.Message, "异常");
            }
  /*          string strClientList = "ADDR               AID CHAN RATE RSSI IDLE  TXSEQ  RXSEQ CAPS ACAPS ERP    STATE HTCAPS\r\n" +
                "e0:ca:94:d4:75:f9    1   11  27M   43    0     57  14256 EPSs         0        7 Q      WME\r\n" +
                "e0:ca:94:d4:75:10    1   11  27M   43    0     57  14256 EPSs         0        7 Q      WME \r\n";
   */         UpdateList(strClientList);
        }

        private void buttonReflash_Click(object sender, EventArgs e)
        {
        //    string strClientList = m_ap.ScanWirelessList();
            /*获取实例
            ADDR               AID CHAN RATE RSSI IDLE  TXSEQ  RXSEQ CAPS ACAPS ERP    STATE HTCAPS
            e0:ca:94:d4:75:f9    1   11  27M   43    0     57  14256 EPSs         0        7 Q      WME
            */
            string strClientList = "";
            try
            {
                strClientList = m_ap.ScanWirelessList();
            }
            catch (Lextm.SharpSnmpLib.Messaging.TimeoutException ex)
            {
                Console.WriteLine(ex.Message.ToString());
                MessageBox.Show("同步超时");
            }
            catch (SocketException ex)
            {
                Console.WriteLine(ex.Message.ToString());
                MessageBox.Show("连接异常");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message.ToString());
                MessageBox.Show(ex.Message, "异常");
            }
            /*          string strClientList = "ADDR               AID CHAN RATE RSSI IDLE  TXSEQ  RXSEQ CAPS ACAPS ERP    STATE HTCAPS\r\n" +
                          "e0:ca:94:d4:75:f9    1   11  27M   43    0     57  14256 EPSs         0        7 Q      WME\r\n" +
                          "e0:ca:94:d4:75:10    1   11  27M   43    0     57  14256 EPSs         0        7 Q      WME \r\n";
             */
            UpdateList(strClientList);
            
        }
        private void UpdateList(string strClientList)
        {
            listViewClients.Items.Clear();
            string[] array = strClientList.Split('\n');
            int count = array.Length;
            for (int i = 1; i < count; i++)
            {
                string[] cloumns = array[i].Split(' ');
                if (cloumns.Length <= 1)
                    continue;
                ListViewItem lvi = new ListViewItem();
                lvi.Text = i.ToString();
                for (int j = 0; j < cloumns.Length; j++)
                {
                    if (cloumns[j].Trim().Equals(""))
                    { //空格忽略
                        continue;
                    }
                    lvi.SubItems.Add(cloumns[j]);
                }
                listViewClients.Items.Add(lvi);
            }
        }
    }
}
